package com.cos.shop.dao.impl;

import com.cos.shop.dao.ProductDao;
import com.cos.shop.entity.Product;
import com.cos.shop.util.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * Copyright (C),2021-2022,亚瑟王
 * FileName: ProductDaoImpl
 * Author: saber
 * Date:2022/9/17 11:01
 */
public class ProductDaoImpl implements ProductDao {

    private QueryRunner qr = new QueryRunner(DBUtils.getDataSource());

    @Override
    public List<Product> getProductByPage(int tid, int startRow, int pageSize)throws SQLException {
        return qr.query("select * from product where tid = ? limit ?,?"
                ,new BeanListHandler<>(Product.class),tid,startRow,pageSize);
    }

    @Override
    public Product getProductByPid(int pid) throws SQLException {
        return qr.query("select * from product where pid =?"
                ,new BeanHandler<>(Product.class),pid);
    }

    @Override
    public Long getProductCoun(int tid) throws SQLException {
        return qr.query("select count(*) from product where tid = ?",new ScalarHandler<>(),tid);
    }
}
